package com.online.fund.spider;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.online.fund.spider.dao.FundDao;
import com.online.fund.spider.entity.SharesRefIdInfo;
import com.online.fund.util.DateUtils;
import com.online.fund.util.HttpClientUtil;

import jdk.nashorn.api.scripting.ScriptObjectMirror;

/**
 * 获取所有上证、深证的股票编码、行业编码、关联id信息
 * @author galen.zhang
 *
 */
@Service
@SuppressWarnings("restriction")
public class SharesRefIdPageProcesser {
	private final static Logger logger = LoggerFactory.getLogger(SharesRefIdPageProcesser.class);
	
	@Resource
	private FundDao fundDao;
   
	private final static String SHARES_TRANSACTION_PAGE_URL = "http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx?type=CT&cmd=#{sharesTypeCode}&sty=FCOIATA&sortType=C&sortRule=-1&page=1&pageSize=#{pageSize}&js=var%20quote_123%3d%7brank:[(x)],pages:(pc)%7d&token=7bc05d0d4c3c22ef9fca8c2a912d779c&jsName=quote_123&_g=0.4888505715865281";
	
	private final static String SHARES_TRANSACTION_PAGE_URL_SH = "http://quote.eastmoney.com/sh#{sharesCode}.html";
	private final static String SHARES_TRANSACTION_PAGE_URL_SZ = "http://quote.eastmoney.com/sz#{sharesCode}.html";

	private static Map<String, String> SHARES_TYPE_MAP = new HashMap<String, String>();

	static {
		SHARES_TYPE_MAP.put("C.2", "上证A股");
		SHARES_TYPE_MAP.put("C.3", "上证B股");
		SHARES_TYPE_MAP.put("C._SZAME", "深证A股");
		SHARES_TYPE_MAP.put("C.7", "深证B股");
	}

	public void parsePage() {
		try {
			parsePage("C.3", "上证B股", true);
			parsePage("C.7", "深证B股", true);
			parsePage("C.2", "上证A股", true);
			parsePage("C._SZAME", "深证A股", true);
			
			HttpClientUtil.closeConnectionPool();
			logger.info("获取所有上证、深证的股票编码、行业编码、关联id信息完成。。。。。。");
		} catch (Exception e) {
			logger.error(e.getMessage());
		}	
	}

	public void parsePage(String sharesTypeCode, String sharesTypeName, boolean saveFlag) {

		try {
			String crawlDate = DateUtils.getCurrentDate();
			String pageSize = "25";
			if (saveFlag) {
				pageSize = "2500";
			}

			String resultStr = HttpClientUtil.httpGetRequest(SHARES_TRANSACTION_PAGE_URL
					.replace("#{sharesTypeCode}", sharesTypeCode).replace("#{pageSize}", pageSize), 5);

			ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
			ScriptEngine nashorn = scriptEngineManager.getEngineByName("nashorn");

			nashorn.eval(resultStr);
			ScriptObjectMirror db = (ScriptObjectMirror) nashorn.get("quote_123");
			ScriptObjectMirror datas = (ScriptObjectMirror) db.get("rank");
			if (datas.isArray()) {
				
				List<SharesRefIdInfo> sharesRefIdInfoList = new ArrayList<SharesRefIdInfo>();
				
				// 删除数据
				if (saveFlag) {
					fundDao.deleteSharesRefIdInfo(sharesTypeCode);
				}
				
				System.out.println(datas.size());
				for (int i = 0; i < datas.size(); i++) {
					String data = (String) datas.get("" + i);
					String[] dataArr = data.split(",");
					
					SharesRefIdInfo entity = new SharesRefIdInfo();
					entity.setCrawlDate(crawlDate);
					entity.setSharesTypeCode(sharesTypeCode);
					entity.setSharesTypeName(sharesTypeName);
					entity.setSharesCode(dataArr[1]);
					entity.setSharesName(dataArr[2]);
					
					String pageHtml = null;
					if ("C.2".equals(sharesTypeCode) || "C.3".equals(sharesTypeCode)) {
						pageHtml = HttpClientUtil.httpGetRequest(SHARES_TRANSACTION_PAGE_URL_SH.replace("#{sharesCode}", entity.getSharesCode()));
					} else {
						pageHtml = HttpClientUtil.httpGetRequest(SHARES_TRANSACTION_PAGE_URL_SZ.replace("#{sharesCode}", entity.getSharesCode()));
					}

//					System.out.println(entity.getSharesCode() + "  " + entity.getSharesName() + "----------------------------------");
//					System.out.println(pageHtml);
		
					Document doc = Jsoup.parse(pageHtml);
					String stockId = null;
					if (doc.select("meta[name='mobile-agent']").hasAttr("content")) {
						String content = doc.select("meta[name='mobile-agent']").eachAttr("content").get(0);
						stockId = content.substring(content.lastIndexOf("/") + 1, content.length() - 5);
					} else {
						String imgSrc = doc.select("#picr img").get(0).attr("src");
						int index = imgSrc.indexOf("id=");
						if (index > 0) {
							//System.out.println(imgSrc.substring(index+3, imgSrc.indexOf("&")));
							stockId = imgSrc.substring(index+3, imgSrc.indexOf("&"));
						}
//						System.out.println(imgSrc);
					}
					
					if (stockId == null) {
						continue;
					} 
					
					entity.setRefId(stockId);
					sharesRefIdInfoList.add(entity);
					
					if (saveFlag) {
						if ((i + 1) % 100 == 0) {
							fundDao.batchAddSharesRefIdInfoList(sharesRefIdInfoList);
							sharesRefIdInfoList.clear();
						}
					} else {
//						System.out.println(data);
						System.out.println(entity);
					}
					
				}
				
				if (saveFlag && !sharesRefIdInfoList.isEmpty()) {
					fundDao.batchAddSharesRefIdInfoList(sharesRefIdInfoList);
				}
				
				// 访问过快会导致服务器出现503错误
				Thread.sleep(500);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static void main(String[] args) throws Exception {
		SharesRefIdPageProcesser processer = new SharesRefIdPageProcesser();
		processer.parsePage("C.2", "上证A股", false);
		
		HttpClientUtil.closeConnectionPool();
	}

}
